<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/WEB-INF/jsp/commons/taglibs.jsp" %>


<div ng-controller="AllDocCtrl">
    <form:form commandName="department" id="inputForm" cssClass="well form-inline" action="${ctx}/adoms/department/save"
               method="post">
        <input type="hidden" name="id" value="${department.id}"/>
        <table class="table table-striped table-bordered table-condensed">
            <thead>
            <i class="icon-flag"></i>
            <strong>部门表单</strong>
            </thead>
            <tr>
                <td class="span2">部门名称:</td>
                <td>
                    <input id="name" name="name" type="text" value="${department.name}" size="30" maxlength="80"/>
                </td>
            </tr>
            <tr>
                <td>部门编号:</td>
                <td>
                    <input id="code" name="code" type="text" value="${department.code}" size="30" maxlength="80"/>
                </td>
            </tr>
            <tr>
                <td>部门类型:</td>
                <td>
                    <form:select path="type.id" id="type" items="${typeList }" itemLabel="name"
                                 itemValue="id"></form:select>
                </td>
            </tr>
            <tr>
                <td>上级部门:</td>
                <td>
                    <select old="${department.department.id }" id="department" name="department.id">
                    </select>
                </td>
            </tr>
            <tr>
                <td class="tBlue">代理品牌:</td>
                <td>
                    <label ng-repeat="brand in brands" class="checkbox">
                        <input ng-model="brand.checked" name="brandIds" value="{{brand.id}}" type="checkbox"/>{{brand.name}}
                    </label>
                </td>
            </tr>
            <tr>
                <td>区域:</td>
                <td>
                    <input id="addrArea" name="addrArea" type="text" value="${department.addrArea}" size="30"
                           maxlength="80"/>
                </td>
            </tr>
            <tr>
                <td>县/区:</td>
                <td>
                    <input id="addrRegion" name="addrRegion" type="text" value="${department.addrRegion}" size="30"
                           maxlength="80"/>
                </td>
            </tr>
            <tr>
                <td>城市:</td>
                <td>
                    <input id="addrCity" name="addrCity" type="text" value="${department.addrCity}" size="30"
                           maxlength="80"/>
                </td>
            </tr>
            <tr>
                <td>省份:</td>
                <td>
                    <input id="addrProvince" name="addrProvince" type="text" value="${department.addrProvince}"
                           size="30"
                           maxlength="80"/>
                </td>
            </tr>
            <tr>
                <td>详细地址:</td>
                <td>
                    <textarea id="addrDetail" name="addrDetail" rows="2"
                              class="span6">${department.addrDetail}</textarea>
                </td>
            </tr>
            <tr>
                <td>联系人:</td>
                <td>
                    <input id="contactor" name="contactor" type="text" value="${department.contactor}" size="30"
                           maxlength="80"/>
                </td>
            </tr>
            <tr>
                <td>联系电话1:</td>
                <td>
                    <input id="contactorPhone1" name="contactorPhone1" type="text" value="${department.contactorPhone1}"
                           size="30" maxlength="80"/>
                </td>
            </tr>
            <tr>
                <td>联系电话2:</td>
                <td>
                    <input id="contactorPhone2" name="contactorPhone2" type="text" value="${department.contactorPhone2}"
                           size="30" maxlength="80"/>
                </td>
            </tr>
            <tr>
                <td>物流名称:</td>
                <td>
                    <input id="directShipper" name="directShipper" type="text" value="${department.directShipper}"
                           size="30"
                           maxlength="80"/>
                </td>
            </tr>
            <tr>
                <td>乘车路线:</td>
                <td>
                    <textarea id="roadMap" name="roadMap" rows="2" class="span6">${department.roadMap}</textarea>
                </td>
            </tr>
            <tr>
                <td>邮编:</td>
                <td>
                    <input id="zipCode" name="zipCode" type="text" value="${department.zipCode}" size="30"
                           maxlength="6"/>
                </td>
            </tr>

            <tr>
                <td>备注:</td>
                <td>
                    <textarea id="memo" name="memo" rows="4" class="span6">${department.memo}</textarea>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input class="btn" type="submit" value="提交"/>
                    &nbsp;
                    <input class="btn" type="button" value="返回" back="back"/>
                </td>
            </tr>
        </table>
    </form:form>
</div>
<script>
    $(function () {

        //聚焦第一个输入框
        $("#name").focus();
        //为inputForm注册validate函数
        $("#inputForm").validate({
            rules: {
                name: {
                    required: true,
                    remote: {
                        type: "POST",
                        url: "${ctx}/adoms/department/repeat",
                        data: {
                            value: function () {
                                return $.trim($("#name").val());
                            },
                            id: ${department.id == null ? 0:department.id},
                            property: "name"
                        }
                    }
                },
                code: {
                    required: true,
                    remote: {
                        type: "POST",
                        url: "${ctx}/adoms/department/repeat",
                        data: {
                            value: function () {
                                return $.trim($("#code").val());
                            },
                            id: '${department.id == null ? 0:department.id}',
                            property: "code"
                        }
                    }
                },
                addrArea: "required",
                addrRegion: "required",
                addrCity: "required",
                addrProvince: "required",
                addrDetail: "required",
                contactor: "required",
//				contactorPhone1 : "required",
//				directShipper : "required",
//				roadMap : "required",
                type: "required",
                department: "required",
                zipCode: {
//					required : true,
                    number: true
                }
            },
            messages: {
                name: {
                    remote: $.format("部门名称重复，请重新输入！")
                }, code: {
                    remote: $.format("部门编号重复，请重新输入！")
                }
            }
        });


        $("#type").change(function () {
            var typeId = $(this).val();
            $.post("${ctx}/adoms/department/listDepartmentByType", {
                typeId: parseInt(typeId) - 1
            }, function (result) {
                var options = "";
                $(result).each(function (i, d) {
                    var old = $("#department").attr("old");
                    var selected = "";
                    if (d.id == old) {
                        selected = "selected";
                    }
                    var option = '<option ' + selected + ' value="' + d.id + '">' + d.name + '</option>';
                    options += option;
                });
                $("#department").html(options);
                $("#department").change();
            }, "json");
        }).change();

    });


</script>

<script>

    (function () {
        var _AllDocCtrl = function ($rootScope, $scope) {

            //加载当前要编辑的部门的商品  -> $rootScope.ecBrands
            loadECBrand($rootScope, $scope);

            $("#department").change(function () {
                var did = $(this).val();
                if (!did) {
                    did = 0;
                }
                loadBrands($rootScope, $scope, did);
            });


        }
        //加载上级部门代理的商品，并选中当前部门所代理的商品品牌
        var loadBrands = function ($rootScope, $scope, did) {
            $.get("${ctx}/adoms/brand/brands/" + did, function (data) {
                $rootScope.brands = data;
                $.each($rootScope.brands, function (i, b) {
                    b.checked = false;
                    if ($rootScope.ecBrands) {
                        $.each($rootScope.ecBrands, function (k, ec) {
                            if (b.id == ec.id) {
                                b.checked = true;
                                return false;
                            }
                        });
                    }
                });
                window.applyAngularScope($rootScope, $scope);
            }, "json");
        }

        //加载当前要编辑的部门所代理的商品
        var loadECBrand = function ($rootScope, $scope) {
            var ecid = "${department.id}";
            if (ecid) {
                $.get("${ctx}/adoms/brand/brands/" + ecid, function (data) {
                    $rootScope.ecBrands = data;
                    window.applyAngularScope($rootScope, $scope);
                }, "json");
            }
        }

        window.AllDocCtrl = _AllDocCtrl;
        AngularApp(window, document);

    })();

</script>

